/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pacificlms.MainPanel;

import com.mysql.jdbc.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.DefaultListModel;
import pacificlms.MasterConnection;
import pacificlms.Verifiable.Database.Copy;
import pacificlms.Verifiable.VerifiableObject;

/**
 *
 * @author Yasir Tanveer
 */
public class SearchForCopy extends CentralUIPanel {

    /**
     * Creates new form SearchForMember
     */
    public SearchForCopy(boolean ShowSelect) {
        super(null);
        initComponents();
        
        btnSelect.setVisible(ShowSelect);
        btnView.setVisible(!ShowSelect);
    }
    public String TabName()
    {
        return "Copy Search";
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel11 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtCopyNumber = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtResourceCode = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtCallNumber = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        btnClose = new javax.swing.JButton();
        btnSearch = new javax.swing.JButton();
        btnSelect = new javax.swing.JButton();
        JScrollPane = new javax.swing.JScrollPane();
        lstSearch = new javax.swing.JList();
        btnView = new javax.swing.JButton();
        cboStatus = new javax.swing.JComboBox();

        setPreferredSize(new java.awt.Dimension(615, 535));

        jLabel11.setFont(new java.awt.Font("Tahoma", 0, 22)); // NOI18N
        jLabel11.setText("Search for Copy");

        jLabel1.setText("Status");

        jLabel2.setText("Copy Number");

        txtCopyNumber.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtCopyNumberActionPerformed(evt);
            }
        });

        jLabel4.setText("Resource Code");

        txtResourceCode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtResourceCodeActionPerformed(evt);
            }
        });

        jLabel5.setText("Call Number");

        txtCallNumber.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtCallNumberActionPerformed(evt);
            }
        });

        jLabel6.setText("Matching Copies");

        btnClose.setText("Close");
        btnClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnClosejButton8ActionPerformed(evt);
            }
        });

        btnSearch.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        btnSearch.setText("Search");
        btnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchjButton8ActionPerformed(evt);
            }
        });

        btnSelect.setText("Select");
        btnSelect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSelectjButton8ActionPerformed(evt);
            }
        });

        JScrollPane.setViewportView(lstSearch);

        btnView.setText("View");
        btnView.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnViewActionPerformed(evt);
            }
        });

        cboStatus.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Available", "Damaged", "Lost", "Retired" }));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, 351, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(14, 14, 14)
                                .addComponent(txtResourceCode, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(12, 12, 12)
                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtCopyNumber, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(46, 46, 46)
                                .addComponent(txtCallNumber, javax.swing.GroupLayout.PREFERRED_SIZE, 356, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(JScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 596, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(btnSelect, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(btnView, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(376, 376, 376)
                                .addComponent(btnClose, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(59, 59, 59)
                                .addComponent(cboStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(306, 306, 306)
                        .addComponent(btnSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(9, 9, 9)
                        .addComponent(jLabel4))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtResourceCode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2)
                            .addComponent(txtCopyNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel5))
                    .addComponent(txtCallNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(cboStatus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(11, 11, 11)
                        .addComponent(jLabel6))
                    .addComponent(btnSearch))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(JScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 341, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnSelect)
                    .addComponent(btnView)
                    .addComponent(btnClose)))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void txtCopyNumberActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtCopyNumberActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtCopyNumberActionPerformed

    private void txtResourceCodeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtResourceCodeActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtResourceCodeActionPerformed

    private void txtCallNumberActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtCallNumberActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtCallNumberActionPerformed

    private void btnClosejButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClosejButton8ActionPerformed
        Close();
    }//GEN-LAST:event_btnClosejButton8ActionPerformed

    void SearchCopy()
    {
        Copy Copy = new Copy();
        Copy.ResourceCode(txtResourceCode.getText());
        Copy.CopyNumber(txtCopyNumber.getText());
        Copy.CallNumber(txtCallNumber.getText());
        Copy.CopyStatus(cboStatus.getSelectedItem().toString());
        
        String SQL = Copy.FindSimilarSQL();
 
        try {
            java.sql.Connection con = MasterConnection.getInstance();
            Statement stmt = con.createStatement();
            
            ResultSet rs;
            rs = stmt.executeQuery(SQL);
            ResultSetMetaData RMS = rs.getMetaData();
            
            DefaultListModel model = new DefaultListModel();
            
            boolean Found = false;
            while (rs.next()) {
                Found = true;
                Copy Cpy = (Copy) VerifiableObject.CreateFromResultSet(Copy.class, rs, RMS);
                model.add(model.getSize(), Cpy);
            }
            if (!Found)
            {
                model.addElement("Copy not found!");
            }
            
            lstSearch.setModel(model);

        }
        catch(Exception Ex)
        {
            System.out.println(Ex.toString());
        }
    }
    
    private void btnSearchjButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchjButton8ActionPerformed
        SearchCopy();
    }//GEN-LAST:event_btnSearchjButton8ActionPerformed

    private void btnSelectjButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSelectjButton8ActionPerformed
        if (lstSearch.getSelectedValue() != null && lstSearch.getSelectedValue().getClass() != String.class) {
            this.CurrentObject = (VerifiableObject) lstSearch.getSelectedValue();
        }
        Close();
    }//GEN-LAST:event_btnSelectjButton8ActionPerformed

    private void btnViewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnViewActionPerformed
        if (lstSearch.getSelectedValue() != null && lstSearch.getSelectedValue().getClass() != String.class)
            this.CurrentObject = (VerifiableObject) lstSearch.getSelectedValue();
        MainPanel.GetMainPanel().AddCentralUIPanel(new InformationCopy(this.CurrentObject));
    }//GEN-LAST:event_btnViewActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane JScrollPane;
    private javax.swing.JButton btnClose;
    private javax.swing.JButton btnSearch;
    private javax.swing.JButton btnSelect;
    private javax.swing.JButton btnView;
    private javax.swing.JComboBox cboStatus;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JList lstSearch;
    private javax.swing.JTextField txtCallNumber;
    private javax.swing.JTextField txtCopyNumber;
    private javax.swing.JTextField txtResourceCode;
    // End of variables declaration//GEN-END:variables
}
